package com.chinairi.mall.controller;


import com.chinairi.common.annotation.DoPage;
import com.chinairi.common.utils.RedisUtil;
import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.controller.viewobject.*;
import com.chinairi.mall.model.CompShop;
import com.chinairi.mall.model.GoodsInfo;
import com.chinairi.mall.model.ServiceShelf;
import com.chinairi.mall.service.ICompShopService;
import com.chinairi.mall.service.IGoodsInfoService;
import com.chinairi.mall.service.IOrderInfoService;
import com.chinairi.mall.service.IServiceShelfService;
import com.chinairi.mall.service.impl.UserCompanyInfoServiceImpl;
import com.chinairi.mall.service.modelobject.*;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.chinairi.common.controller.BaseController;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 企业店铺表 前端控制器
 * </p>
 *
 * @author gsl
 * @since 2020-09-06
 */
@RestController
@RequestMapping("/comp-shop")
public class CompShopController extends BaseController {

    @Autowired
    private ICompShopService iCompShopService;
    @Autowired
    private IGoodsInfoService iGoodsInfoService;
    @Autowired
    private IServiceShelfService iServiceShelfService;
    @Autowired
    private IOrderInfoService iOrderInfoService;

    /**
     * 新建店铺
     *
     * @param compShop
     * @throws IOException
     */
    @PostMapping("/add")
    public void add(@RequestBody CompShop compShop) throws IOException {
        iCompShopService.add(compShop);
        ResponseUtil.showMessage("success", "新建店铺成功", "");
    }

    /**
     * 更新店铺信息
     *
     * @param compShop
     * @throws IOException
     */
    @PutMapping("/update")
    public void update(@RequestBody CompShop compShop) throws IOException {
        iCompShopService.updateShop(compShop);
        ResponseUtil.showMessage("success", "更新成功", "");
    }

    /**
     * 删除店铺
     *
     * @param csDbid 店铺id
     * @param flag   店铺种类：0商品店铺，1服务店铺
     * @throws IOException
     */
    @DeleteMapping("deleteById")
    public void deleteById(@RequestParam("csDbid") String csDbid, @RequestParam("flag") String flag) throws IOException {
        iCompShopService.deleteById(csDbid, flag);
        ResponseUtil.showMessage("success", "删除成功", "");
    }

    /**
     * 根据店铺类别获取用户的企业下的店铺
     *
     * @param type 类别，前端回传
     * @throws IOException 异常
     */
    @GetMapping("/getAllByType")
    public void getAllByType(String type) throws IOException {
        CompShopVO compShopVO = iCompShopService.getAllByType(type);
        ResponseUtil.showMessage("success", "获取成功", compShopVO);
    }

    /**
     * 根据店铺类别获取所有的企业的店铺
     *
     * @param compShopMo 类别，前端回传
     * @throws IOException 异常
     */
    @GetMapping("/getAllShopByType")
    public void getAllShopByType(CompShopMO compShopMo) throws IOException {
        List<CompShopMO> compShopMoList = iCompShopService.getAllShopByType(compShopMo);
        ResponseUtil.showMessage("success", "获取成功", compShopMoList);
    }

    /**
     * 获取所有含四新商品的店铺
     *
     * @param compShopMo 类别，前端回传
     * @throws IOException 异常
     */
    @GetMapping("/getFourNewShop")
    public void getFourNewShop(CompShopMO compShopMo) throws IOException {
        List<CompShopMO> compShopMoList = iCompShopService.getFourNewShop(compShopMo);
        ResponseUtil.showMessage("success", "获取成功", compShopMoList);
    }

    /**
     * 根据店铺id获取店铺下所有的商品信息，接口中回传店铺id和店铺类型
     * impl中根据类型进入不同的impl方法
     * 用同一个接收体混合抛出。
     *
     * @param compShopMo 类别，前端回传
     * @throws IOException 异常
     */
    @GetMapping("/getAllGoodsByShopId")
    public void getAllGoodsByShopId(CompShopMO compShopMo) throws IOException {
        List<GoodsAndServiceVO> goodsAndServiceVoList = iCompShopService.getAllGoodsByShopId(compShopMo);
        ResponseUtil.showMessage("success", "获取成功", goodsAndServiceVoList);
    }

    /**
     * 根据店铺id获取店铺下的四新商品信息，接口中回传店铺id
     *
     * @param compShopMo 类别，前端回传
     * @throws IOException 异常
     */
    @GetMapping("/getFourNewGoodsByShopId")
    public void getFourNewGoodsByShopId(CompShopMO compShopMo) throws IOException {
        List<GoodsAndServiceVO> goodsAndServiceVoList = iCompShopService.getFourNewGoodsByShopId(compShopMo);
        ResponseUtil.showMessage("success", "获取成功", goodsAndServiceVoList);
    }

    /**
     * 单个商品审核
     * 回传商品id/服务商品id，回传type来标识
     *
     * @param goodsAndServiceVo
     * @throws IOException
     */
    @PutMapping("/updateGoodsOrServiceStatus")
    public void updateGoodsOrServiceStatus(@RequestBody GoodsAndServiceVO goodsAndServiceVo) throws IOException {
        iCompShopService.updateGoodsOrServiceStatus(goodsAndServiceVo);
        ResponseUtil.showMessage("success", "商品上下架审核成功", "");
    }

    /**
     * 单个四新商品审核
     *
     * @param goodsAndServiceVo
     * @throws IOException
     */
    @PutMapping("/updateFourNewGoodsStatus")
    public void updateFourNewGoodsStatus(@RequestBody GoodsAndServiceVO goodsAndServiceVo) throws IOException {
        iCompShopService.updateFourNewGoodsStatus(goodsAndServiceVo);
        ResponseUtil.showMessage("success", "四新商品上下架审核成功", "");
    }

    /**
     * 店铺商品全部审核
     *
     * @param compShopMo
     * @throws IOException
     */
    @PutMapping("/updateGoodsOrServiceStatusByCsDbId")
    public void updateGoodsOrServiceStatusByCsDbId(@RequestBody CompShopMO compShopMo) throws IOException {
        iCompShopService.updateGoodsOrServiceStatusByCsDbId(compShopMo);
        ResponseUtil.showMessage("success", "批量审核成功", null);
    }

    /**
     * 店铺四新商品全部审核
     *
     * @param compShopMo
     * @throws IOException
     */
    @PutMapping("/updateFourNewGoodsStatusByCsDbId")
    public void updateFourNewGoodsStatusByCsDbId(@RequestBody CompShopMO compShopMo) throws IOException {
        iCompShopService.updateFourNewGoodsStatusByCsDbId(compShopMo);
        ResponseUtil.showMessage("success", "四新商品批量审核成功", "");
    }

    /**
     * 根据店铺id和其他信息搜索店铺的服务
     *
     * @param orderCollectionVO
     * @return
     */
    @GetMapping("/searchShopOrderServiceCollection")
    public void searchShopOrderServiceCollection(OrderCollectionVO orderCollectionVO) throws IOException {
        String type = orderCollectionVO.getType();
        OrderServiceCollectionVO orderServiceCollectionVO = new OrderServiceCollectionVO();
        if ("2".equals(type)) {
            orderCollectionVO.setShopId(iCompShopService.getAllByType("2").getCsDbid());
            orderServiceCollectionVO.setOrderServicePurchaseList(iCompShopService.searchShopOrderPurchaseCollection(orderCollectionVO));
        } else if ("3".equals(type)) {
            orderCollectionVO.setShopId(iCompShopService.getAllByType("3").getCsDbid());
            orderServiceCollectionVO.setOrderServiceQualityList(iCompShopService.searchShopOrderQualityCollection(orderCollectionVO));
        } else if ("6".equals(type)) {
            orderCollectionVO.setShopId(iCompShopService.getAllByType("6").getCsDbid());
            orderServiceCollectionVO.setOrderServiceSettleList(iCompShopService.searchShopOrderSettleCollection(orderCollectionVO));
        } else if ("5".equals(type)) {
            orderCollectionVO.setShopId(iCompShopService.getAllByType("5").getCsDbid());
            orderServiceCollectionVO.setOrderServiceStorageList(iCompShopService.searchShopOrderStorageCollection(orderCollectionVO));
        } else if ("4".equals(type)) {
            orderCollectionVO.setShopId(iCompShopService.getAllByType("4").getCsDbid());
            orderServiceCollectionVO.setOrderServiceTransportList(iCompShopService.searchShopOrderTransportCollection(orderCollectionVO));
        }
        ResponseUtil.showMessage("success", "获取成功", orderServiceCollectionVO);
    }

    /**
     * 根据服务订单id获取订单下的商品信息
     *
     * @param orderCollectionMO
     * @throws IOException
     */
    @GetMapping("/getOrderServiceGoodsById")
    public void getOrderServiceGoodsById(OrderServiceGoodsMO orderCollectionMO) throws IOException {
        OrderServiceGoodsVO orderServiceGoodsVO = iCompShopService.getOrderServiceGoodsById(orderCollectionMO);
        ResponseUtil.showMessage("success", "获取成功", orderServiceGoodsVO);
    }

    /**
     * 店铺拒绝服务订单
     *
     * @param orderCollectionMO
     * @throws IOException
     */
    @GetMapping("/refuseOrderService")
    public void refuseOrderService(OrderServiceGoodsMO orderCollectionMO) throws IOException {
        iCompShopService.refuseOrderService(orderCollectionMO);
        ResponseUtil.showMessage("success", "拒绝订单成功", "");
    }

    /**
     * 店铺确认服务订单
     *
     * @param orderCollectionMO
     * @throws IOException
     */
    @GetMapping("/confirmOrderService")
    public void confirmOrderService(OrderServiceGoodsMO orderCollectionMO) throws IOException {
        iCompShopService.confirmOrderService(orderCollectionMO);
        ResponseUtil.showMessage("success", "确认订单成功", "");
    }

    /**
     * csShopName  模糊搜索店铺名称
     * csShopType  精确搜索店铺类别
     *
     * @param homeShopMo
     * @throws IOException
     */
    @GetMapping("/getHomeShop")
    public void getHomeShop(HomeShopMO homeShopMo) throws IOException {
        List<HomeShopMO> homeShopMOList = iCompShopService.getHomeShop(homeShopMo);
        ResponseUtil.showMessage("success", "获取成功", homeShopMOList);
    }

    /**
     * 店铺管理，获取店铺列表。
     *
     * @param compShopManageMo
     * @throws IOException
     */
    @GetMapping("/getAllCompShopManageList")
    public void getAllCompShopManageList(CompShopManageMO compShopManageMo) throws IOException {
        List<CompShopManageMO> compShopManageMoList = iCompShopService.getAllCompShopManageList(compShopManageMo);
        ResponseUtil.showMessage("success", "获取成功", compShopManageMoList);
    }

    /**
     * 获取店铺所有商品
     *
     * @param homeGoodsMo
     * @throws IOException
     */
    @GetMapping("/getGoodsListByCsDbId")
    public void getGoodsListByCsDbId(HomeGoodsMO homeGoodsMo) throws IOException {
        List<HomeGoodsMO> list = iGoodsInfoService.getGoodsListByCsDbId(homeGoodsMo);
        ResponseUtil.showMessage("success", "获取成功", list);
    }

    /**
     * 获取店铺所有订单
     *
     * @param orderInfoMo
     * @throws IOException
     */
    @GetMapping("/getGoodsOrderListByCsDbId")
    public void getGoodsOrderListByCsDbId(OrderInfoMO orderInfoMo) throws IOException {
        List<OrderInfoMO> list = iOrderInfoService.getOrderInfoByCsDbId(orderInfoMo);
        ResponseUtil.showMessage("success", "获取成功", list);
    }

    /**
     * 获取店铺所有商品
     *
     * @param serviceShelf
     * @throws IOException
     */
    @GetMapping("/getServiceListByCsDbId")
    public void getServiceListByCsDbId(ServiceShelf serviceShelf) throws IOException {
        List<ServiceShelf> list = iServiceShelfService.getServiceListByCsDbId(serviceShelf);
        ResponseUtil.showMessage("success", "获取成功", list);
    }

    /**
     * 获取店铺所有订单
     *
     * @param orderCollectionVo
     * @throws IOException
     */
    @GetMapping("/getServiceOrderListByCsDbId")
    public void getGoodsOrderListByCsDbId(OrderCollectionVO orderCollectionVo) throws IOException {
        String type = orderCollectionVo.getType();
        OrderServiceCollectionVO orderServiceCollectionVO = new OrderServiceCollectionVO();
        if ("2".equals(type)) {
            orderServiceCollectionVO.setOrderServicePurchaseList(iCompShopService.searchShopOrderPurchaseCollection(orderCollectionVo));
        } else if ("3".equals(type)) {
            orderServiceCollectionVO.setOrderServiceQualityList(iCompShopService.searchShopOrderQualityCollection(orderCollectionVo));
        } else if ("6".equals(type)) {
            orderServiceCollectionVO.setOrderServiceSettleList(iCompShopService.searchShopOrderSettleCollection(orderCollectionVo));
        } else if ("5".equals(type)) {
            orderServiceCollectionVO.setOrderServiceStorageList(iCompShopService.searchShopOrderStorageCollection(orderCollectionVo));
        } else if ("4".equals(type)) {
            orderServiceCollectionVO.setOrderServiceTransportList(iCompShopService.searchShopOrderTransportCollection(orderCollectionVo));
        }
        ResponseUtil.showMessage("success", "获取成功", orderServiceCollectionVO);
    }

    /**
     * 获取审核记录
     * @param csDbId 店铺id
     * @param flag 0：商品，1：服务，2：四新
     */
    @GetMapping("/getApproveRecorder")
    public void getApproveRecorder(@RequestParam("csDbId") String csDbId,@RequestParam("flag") String flag,ApproveSearchVO approveSearchVO) throws IOException {
        Object resultList = iCompShopService.getApproveRecorder(csDbId,flag,approveSearchVO);
        ResponseUtil.showMessage("success", "获取成功",resultList);
    }

    /**
     * 获取有审核记录的店铺列表
     * @throws IOException
     */
    @GetMapping("/getApproveShopList")
    public void getApproveShopList(String shopTypeFlag,CompShopMO compShopMO) throws IOException{
        List<CompShopMO> shopList = iCompShopService.getApproveShopList(shopTypeFlag,compShopMO);
        ResponseUtil.showMessage("success","获取成功",shopList);
    }
}
